package calculate24.play.SelfCalculate;

import android.content.Context;
import android.view.MenuItem;
import android.widget.PopupMenu;

import calculate24.play.R;
import calculate24.play.common.card.Card;

//弹窗供用户选择运算
abstract class PopupCalculationMenu {

    public static void create(Context context, Card card1, Card card2, onResultListener l) {
        PopupMenu popupMenu = new PopupMenu(context, card2.view);
        popupMenu.getMenuInflater().inflate(R.menu.empty, popupMenu.getMenu());
        popupMenu.getMenu().add(card1.number + " + " + card2.number)
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                l.onResult(card1.number + card2.number);
                return false;
            }
        });
        popupMenu.getMenu().add(card1.number + " - " + card2.number)
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        l.onResult(card1.number - card2.number);
                        return false;
                    }
                });
        popupMenu.getMenu().add(card1.number + " * " + card2.number)
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        l.onResult(card1.number * card2.number);
                        return false;
                    }
                });
        if(card1.number % card2.number == 0) {
            popupMenu.getMenu().add(card1.number + " / " + card2.number)
                    .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem menuItem) {
                            l.onResult(card1.number / card2.number);
                            return false;
                        }
                    });
        }
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu popupMenu) {
                card1.drop();
                card2.drop();
            }
        });
        popupMenu.show();
    }

    public static interface onResultListener {
        void onResult(int result);
    }
}
